package demo1;

import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2022/2/25
 * Time: 20:29
 * Description:
 */
class A {
    public int id;
    public A(int id) {
        this.id = id;
    }
    public boolean equals(Object obj) {
        //根据你自己打规则 判断是不是一样的两个对象
        if (obj == null) {
            return false ;
        }
        if(this == obj) {
            return true ;
        }
        // 不是A类对象
        if (!(obj instanceof A)) {
            return false ;
        }
        A tmp = (A) obj ; // 向下转型，比较属性值
        return this.id == tmp.id ;
    }
    //重写 归 重写  不敢胡来  除非你的业务需求就是这个样子
    public boolean equalsError(Object obj) {
        return true;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}

public class TestDemo4 {

    public static void main(String[] args) {
        Object[] array = {1,2,"hello",67.8};
        //Integer[] tmp = (Object[])array;
    }

    public static void main5(String[] args) {
        // Object接收数组对象，向上转型
        Object obj = new int[]{1,2,3,4,5,6} ;
        // 向下转型，需要强转
        int[] data = (int[]) obj ;
        for(int i :data){
            System.out.print(i+"、");
        }
    }

    public static void main4(String[] args) {
        A a1 = new A(1);
        A a2 = new A(1);
        System.out.println(a1.hashCode());
        System.out.println(a2.hashCode());
    }

    public static void main3(String[] args) {
        A a1 = new A(1);
        String str = "123";
        System.out.println(a1.equals(str));
    }

    public static void main2(String[] args) {
        A a1 = new A(1);
        A a2 = a1;
        System.out.println(a1.equals(a2));
    }


    public static void main1(String[] args) {
        A a1 = new A(1);

        A a2 = new A(1);
        System.out.println(a1 == a2);
        System.out.println(a1.equals(a2));

    }

    public static void func(Object obj) {

    }
}
